package io.lx.wechat;

import com.sun.deploy.util.StringUtils;
import io.lx.wechat.util.JSONResult;
import okhttp3.*;

import java.io.*;
import java.util.Random;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;

/**
 * @Author: lx
 * @Date: 2019/6/25 0025 下午 17:14
 */
public class WechatTest {
    OkHttpClient client = new OkHttpClient();




    //    appid	wx782c26e4c19acffb	固定值
//    fun	new	固定值
//    lang	zh_CN
    public String getUUid() {
        Request request = new Request.Builder()
                .url("https://login.wx.qq.com/jslogin?appid=wx782c26e4c19acffb&fun=new&lang=zh_CN")
                .build();
        try (Response response = client.newCall(request).execute()) {
            // 不能直接返回，要将uuid截取出来，可以使用正则表达式
            // 先把这里做了把
            String str = response.body().string();
            str = str.replace("window.QRLogin.code = 200; window.QRLogin.uuid = \"", "")
                    .replace("\";", "");
            return str;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    // https://login.wx.qq.com/qrcode/{uuid}
    public void getQRCode(String uuid) {
        String url = "https://login.wx.qq.com/qrcode/" + uuid+"?t=webwx";
        System.out.println(url);
        // 就这里
        Request request = new Request.Builder()
                .url(url)
                .build();
        try (Response response = client.newCall(request).execute()) {
            String body = response.body().string();
            //byte[] bytes = body.bytes();
            //System.out.println(bytes.length);

            FileInputStream fis = null;
            FileOutputStream fos = null;

            String path= null;
            try {
                path = "D:/work/cloud/cc.jpg";

                File file = new File(path);
                PrintStream ps = new PrintStream(new FileOutputStream(file));
                ps.println(body);// 往文件里写入字符串

            } catch (Exception e) {
                e.printStackTrace();
            }finally {
                if (fis != null){
                    try {
                        fis.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                if (fos != null){
                    try {
                        fos.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }

            }

            //PrintStream ps = new PrintStream(new FileOutputStream(file));
            //ps.println(body);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public String login(String uuid){
        client = new OkHttpClient.Builder()
                .connectTimeout(5000, TimeUnit.SECONDS)
                .readTimeout(5000, TimeUnit.SECONDS)
                .build();

        String url = "https://login.wx.qq.com/cgi-bin/mmwebwx-bin/login?uuid="+uuid+"&tip=1";

        System.out.println(url);

        Request request = new Request.Builder()
                .url(url)
                .build();

        try (Response response = client.newCall(request).execute()){
            String body = response.body().string();

            String str = body.replace("window.code=200;window.redirect_uri=\"","").
                    replace("\";","");
            System.out.println(str);

            return str;
            //String str = body.split("");
        } catch (IOException e) {
            e.printStackTrace();
        }

        return null;

    }

    public JSONResult initLogin(){

        String url="https://wx.qq.com/cgi-bin/mmwebwx-bin/webwxinit?pass_ticket=qg%2BL%2BfjNnoHyqYsL3xj0KoNi5nqchsInPWWSxDwqCJ8%3D";
        String DeviceID = "e" + String.valueOf(new Random().nextLong()).substring(1, 16);
        FormBody formBody = new FormBody.Builder()
                .add("Uin","211722515")
                .add("Sid","+urBrYI292xoIknf")
                .add("Skey","@crypt_8b4f09cc_a5871dc10130a48703b9afd5602152e4")
                .add("DeviceID",DeviceID)
                .build();
        Request request;
        request = new Request.Builder()
                .post(formBody)
                .url(url)
                .build();
        try {
            Response response = client.newCall(request).execute();
            String body = response.body().string();
            return JSONResult.success(body);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;

    }



    public static void main(String[] args) {
        WechatTest wechat = new WechatTest();
        String uuid = wechat.getUUid();
        System.out.println(uuid);
        wechat.getQRCode(uuid);
        try {
            Thread.sleep(10000);
        } catch (InterruptedException e) {


        }
        wechat.login(uuid);
        wechat.initLogin();
    }
}
